package com.hup.utils.swing.customView.colorChooser;

import com.hup.utils.commons.start.LookThemeUtil;
import lombok.extern.log4j.Log4j2;

import java.awt.*;
import java.awt.image.MemoryImageSource;

/**
 * [圆形]彩色版;
 * todo 未完成,不会画圆
 * @author hugan
 * @date 2020/4/10
 */
@Log4j2
public class CircleColorLabel extends AbsColorLabel {

    /**
     * C=周长=6个点+6*[0~255内的点]=6*256=1530
     * d=直径=C/pi=487.01
     *   487*pi=1529.95,此时外圆漏了一个色点
     *   488足够,但是导致坐标中圆心不是整数位
     *   所以取489
     * d=244+1+244
     * R=244
     */
    private static final int pixW = 489;
    /**
     * {@link #pix} 第245个点是圆心
     */
    private static final int O_INDEX = 244;
    private static final int CIRCLE_R = 244;

    /**
     * 亮度
     */
    private int brightness = 0;

    public CircleColorLabel() {
        int w = (int) (pixW * LookThemeUtil.getScreenScale());
        Dimension dimension = new Dimension(w, w);
        setPreferredSize(dimension);
        setMinimumSize(dimension);
    }

    @Override
    protected int[] initPix() {
        return new int[pixW * pixW];
    }

    @Override
    protected boolean needUpdateImage(Color newer, Color older) {
        //todo 计算亮度,根据亮度判断是否需要更新图像
        return true;
    }

    @Override
    protected Image updateImageByColor() {
        log.info("");
        for (int r = 0; r < pixW; r++) {//row
            for (int c = 0; c < pixW; c++) {//column
                int x = c - O_INDEX;//[-R,+R]
                int y = r - O_INDEX;
                //todo double
                int pointR = (int) Math.sqrt(x * x + y * y);
                int i = r * pixW + c;
                if (pointR <= CIRCLE_R) {
                    pix[i] = 123 << 24 | 222 << 16 | 33 << 8 | 111;
                //} else if(pointR == CIRCLE_R + 1) {
                //    pix[i] = 123 << 24 | 222 << 16 | 23 << 8 | 221;
                }
            }
        }
        return createImage(new MemoryImageSource(pixW, pixW, pix, 0, pixW));
    }

    @Override
    protected boolean doPaintGraphics(Graphics g) {
        //log.info("w={}, h={}", width, height);
        //背景填充当前颜色
        g.setColor(color);
        g.fillRect(0, 0, width, height);

        //抗锯齿,都有效果
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //绘制[彩圆]和[外环],并适配宽高居中绘制
        int circlePadding = 10;//[彩圆]单侧距离[最近的边界]的距离
        int d;//[彩圆]直径
        if (width < height) {
            d = width - circlePadding * 2;
        } else {
            d = height - circlePadding * 2;
        }
        int outerD = d + circlePadding;//[外环]直径

        if (width < height) {
            g.setColor(getBackground());
            g.fillOval(circlePadding / 2, (height - outerD) / 2, outerD, outerD);
            g.drawImage(image, circlePadding, (height - d) / 2, d, d, null);
        } else {
            g.setColor(getBackground());
            g.fillOval((width - outerD) / 2, circlePadding / 2, outerD, outerD);
            g.drawImage(image, (width - d) / 2, circlePadding, d, d, null);
        }
        //todo 绘制取色图标
        return true;
    }
}
